# -*- coding: utf-8 -*-
# @File  : LC938.py
# @Author: Yin
# @Date  : 2021/4/27
# @Desc  :

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def __init__(self):
        self.ans = 0
        self.low = 0
        self.high = 0

    def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
        self.low = low
        self.high = high
        if root is None:
            return 0
        self.dfs(root)
        return self.ans

    def dfs(self, node: TreeNode):
        if node is None:
            return
        if self.low <= node.val <= self.high:
            self.ans += node.val
        self.dfs(node.left)
        self.dfs(node.right)
